package com.yylending.las.lending.process;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.lending.increase.CheckPromoteStatusActivity;
import com.yylending.las.lending.increase.IncreaseModel;
import com.yylending.las.lending.increase.IncreasePresenter;

/**
 * 寿险保单提额
 * Created by Pengrf on 2017/11/17.
 * Copyright  2017 www.yyfax.com. All Rights Reserved.
 */
public class IncreaseLimitByPolicyActivity extends BaseActivity implements IncreasePresenter.View {

	private static final int LOADER_ID_INCREASE_LIMIT = 10001;
	private static final String KEY_NEED_GOBACK = "key_need_goback";
	private static final String TAG = IncreaseLimitByPolicyActivity.class.getSimpleName();
	@BindView(R.id.pingan_account_edt)
	EditText mPinganAccountEdt;
	@BindView(R.id.pingan_account_delete)
	ImageView mPinganAccountDelete;
	@BindView(R.id.pingan_account_password_edt)
	EditText mPinganAccountPasswordEdt;
	@BindView(R.id.pingan_account_password_chb)
	CheckBox mPinganAccountPasswordChb;
	@BindView(R.id.pingan_account_password_delete)
	ImageView mPinganAccountPasswordDelete;
	/**
	 * 平安一账通用户协议
	 */
	@BindView(R.id.pingan_account_chb)
	CheckBox mPinganAccountChb;
	@BindView(R.id.pingan_account_user_agreement)
	TextView mPinganAccountUserAgreement;
	@BindView(R.id.pingan_account_login_submit_btn)
	Button mPinganAccountLoginSubmitBtn;

	private IncreasePresenter mIncreasePresenter;

	IncreaseModel.IncreaseParams mIncreaseParams = new IncreaseModel.IncreaseParams("", "", "", "");

	private boolean mBackToPreIncreaseActivity = false;

	public static void start(Activity activity, boolean backToPreIncreaseActivity) {
		Intent intent = new Intent(activity, IncreaseLimitByPolicyActivity.class);
		intent.putExtra(KEY_NEED_GOBACK, backToPreIncreaseActivity);
		activity.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_increase_limit_by_policy);
		ButterKnife.bind(this);
		mBackToPreIncreaseActivity = getIntent().getBooleanExtra(KEY_NEED_GOBACK, false);
		setTitle(R.string.check_policy);
		initView();
		getSupportLoaderManager().initLoader(LOADER_ID_INCREASE_LIMIT, null, this);
	}

	private void initView() {
		mPinganAccountEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().trim().length() != 0) {
					mPinganAccountDelete.setVisibility(View.VISIBLE);
				} else {
					mPinganAccountDelete.setVisibility(View.GONE);
				}
			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mPinganAccountPasswordEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().trim().length() != 0) {
					mPinganAccountPasswordDelete.setVisibility(View.VISIBLE);
				} else {
					mPinganAccountPasswordDelete.setVisibility(View.GONE);
				}
			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mPinganAccountChb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mPinganAccountLoginSubmitBtn.setEnabled(true);
				} else {
					mPinganAccountLoginSubmitBtn.setEnabled(false);
				}
			}
		});

		mPinganAccountPasswordChb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mPinganAccountPasswordEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				} else {
					mPinganAccountPasswordEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
				}
				mPinganAccountPasswordEdt.setSelection(mPinganAccountPasswordEdt.getText().length());
			}
		});
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID_INCREASE_LIMIT:
				mIncreasePresenter = (IncreasePresenter) data;
				mIncreasePresenter.attach(this);
				break;
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID_INCREASE_LIMIT:
				return new PresenterLoader<>(this, new IncreasePresenter.Generator());
			default:
				return null;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onDestroy() {
		getSupportLoaderManager().destroyLoader(LOADER_ID_INCREASE_LIMIT);
		super.onDestroy();
	}

	@Override
	public void promoteByPolicySuccess() {
		CheckPromoteStatusActivity.start(this);
		finish();
	}

	@Override
	public void promoteByPolicyFail(String msg) {
		showToast(msg);
	}

	@OnClick({R.id.pingan_account_delete, R.id.pingan_account_password_delete, R.id.pingan_account_user_agreement, R.id.pingan_account_login_submit_btn})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.pingan_account_delete:
				mPinganAccountEdt.getText().clear();
				break;
			case R.id.pingan_account_password_delete:
				mPinganAccountPasswordEdt.getText().clear();
				break;
			case R.id.pingan_account_user_agreement:
				mPinganAccountChb.setChecked(!mPinganAccountChb.isChecked());
				if (mPinganAccountChb.isChecked()) {
					mPinganAccountLoginSubmitBtn.setEnabled(true);
				} else {
					mPinganAccountLoginSubmitBtn.setEnabled(false);
				}
				break;
			case R.id.pingan_account_login_submit_btn:
				StatService.onEvent(IncreaseLimitByPolicyActivity.this, StatIDSet.EVENT_ID_INCREASE_BY_POLICY_SUBMIT);
				//提交,发起网络请求
				verifyAccount();
				break;

		}
	}

	private void verifyAccount() {
		if (TextUtils.isEmpty(mPinganAccountEdt.getText()) || TextUtils.isEmpty(mPinganAccountPasswordEdt.getText())) {
			showToast(getString(R.string.please_input_toast, getString(R.string.account_and_password)));
			return;
		}

		//发起网络请求
		mIncreaseParams.name = mPinganAccountEdt.getText().toString().trim();
		mIncreaseParams.pass = mPinganAccountPasswordEdt.getText().toString().trim();
		mIncreaseParams.assort = Constants.INCREASE_LIMIT_TYPE_POLICY;
		mIncreaseParams.type = Constants.POLICY_TYPE_PING_AN;
		mIncreasePresenter.increaseLimitByPolicy(mIncreaseParams);
	}

	@Override
	protected void onBackBtnPressed() {
		super.onBackBtnPressed();
		judgeBack();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		judgeBack();
	}

	/**
	 * 判断是否需要返回到提额类型选择界面
	 */
	private void judgeBack(){
		if(mBackToPreIncreaseActivity){
			PreIncreaseLimitActivity.start(this);
			overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
		}
	}
}